/**************************************************************************************************
* Title: SumColor_16f.cpp
* Author: Gael Huber
* Description: Defines various functions pertaining to colors
**************************************************************************************************/
#include "SumMath.h"

/**************************************************************************************************
* Interpolate r,g,b between desaturated color and color. 
* DesaturatedColor + s(Color - DesaturatedColor)
**************************************************************************************************/
Color_16f* ColorAdjustSaturation_16f(Color_16f* pOut, const Color_16f* pC, float s) {
	const float grey(pC->r*0.2125f + pC->g*0.7154f + pC->b*0.0721f);
	Color_16f desat(grey, grey, grey, pC->a);
	Color_16f diff;
	ColorSub_16f(&diff, pC, &desat);
	ColorScale_16f(&diff, &diff, s);
	ColorAdd_16f(pOut, &desat, &diff);
	return pOut;
}

/**************************************************************************************************
* Interpolate r,g,b between desaturated color and color. 
* DesaturatedColor + s(Color - DesaturatedColor)
**************************************************************************************************/
Color_16f* ColorAdjustSaturation_16f(Color_16f* pOut, const Color_16f* pC, const __m128& s) {
	const float grey(pC->r*0.2125f + pC->g*0.7154f + pC->b*0.0721f);
	Color_16f desat(grey, grey, grey, pC->a);
	Color_16f diff;
	ColorSub_16f(&diff, pC, &desat);
	ColorScale_16f(&diff, &diff, s);
	ColorAdd_16f(pOut, &desat, &diff);
	return pOut;
}

/**************************************************************************************************
* Interpolate r,g,b, between 50% grey and color. 
* Grey + c(Color - Grey)
**************************************************************************************************/
Color_16f* ColorAdjustContrast_16f(Color_16f* pOut, const Color_16f* pC, float c) {
	__m128 m(_mm_set1_ps(0.5f));
	pOut->v = _mm_add_ps(m, _mm_mul_ps(_mm_load1_ps(&c), _mm_sub_ps(pC->v, m)));
	pOut->a = pC->a;
	return pOut;
}

/**************************************************************************************************
* Interpolate r,g,b, between 50% grey and color. 
* Grey + c(Color - Grey)
**************************************************************************************************/
Color_16f* ColorAdjustContrast_16f(Color_16f* pOut, const Color_16f* pC, const __m128& c) {
	__m128 m(_mm_set1_ps(0.5f));
	pOut->v = _mm_add_ps(m, _mm_mul_ps(c, _mm_sub_ps(pC->v, m)));
	pOut->a = pC->a;
	return pOut;
}